package com.yunji.order.dao.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.common.annotation.DataScope;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.order.bo.OrderScaleBO;
import com.yunji.order.dao.IOrderScaleDao;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.OrderScaleDO;
import com.yunji.order.dto.OrderScaleParam;
import com.yunji.order.mapper.IOrderScaleMapper;
import com.yunji.order.vo.OrderScaleVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/16 15:29
 * @description : TODO
 **/
@Repository
public class OrderScaleDaoImpl extends BaseDaoImpl<IOrderScaleMapper, OrderScaleDO> implements IOrderScaleDao {

    @Override
    public List<OrderScaleDO> getOrderScaleList(OrderScaleBO param) {
        return lambdaQuery()
                .eq(param.getOrderId() != null, OrderScaleDO::getOrderId, param.getOrderId())
                .eq(StringUtils.hasText(param.getDataCode()), OrderScaleDO::getDataCode, param.getDataCode())
                .in(!CollectionUtils.isEmpty(param.getScaleTypeList()), OrderScaleDO::getTradeType, param.getScaleTypeList()).list();
    }

    @DataScope
    @Override
    public List<OrderScaleVO> orderScaleVOList(OrderScaleParam param) {
        MPJLambdaWrapper<OrderScaleDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(OrderScaleDO.class).leftJoin(OrderDO.class, OrderDO::getOrderId, OrderScaleDO::getOrderId)
                .select(OrderDO::getContainerName).select(OrderDO::getActualAmount).select(OrderDO::getPayTime)
                .select(OrderDO::getMemberId).select(OrderDO::getMemberPhone).select(OrderDO::getMemberName)
                .like(StringUtils.hasText(param.getContainerName()), OrderDO::getContainerName, param.getContainerName())
                .like(StringUtils.hasText(param.getAccountName()), OrderScaleDO::getAccountName, param.getAccountName())
                .like(StringUtils.hasText(param.getOrderCode()), OrderDO::getOrderCode, param.getOrderCode())
                .eq(param.getAccountType() != null, OrderScaleDO::getAccountType, param.getAccountType())
                .orderByDesc(OrderScaleDO::getCreateTime);
        return pageJoinList(OrderScaleVO.class, param, wrapper);
    }
}
